<html>
    <head>
        <title>JSUnit - RosterWindowTest</title>
        <script language="javascript" src="app/jsUnitCore.js"></script>
        
                <script type="text/javascript" src="includes.js"></script>

    </head>
    <body>
        <script type="text/javascript">

var GROUP1 = "Group 1";
var GROUP2 = "Group 2";

var JID1 = "somebody@somewhere.com";
var JID2 = "somebody2@somewhere.com";

var JID_WITH_RESOURCE = JID1+"/resource1";

var tree;

function setUp() {
    tree = new Xmpp4Js.UI.Roster.RosterTree({
        renderTo: document.body
    });
}

function tearDown() {
    delete tree;
}

/**
 * Try to create a group and make sure that it exists.
 */
function testCreateGroup() {
    tree.createGroup( GROUP1 );
    
    var group = tree.getGroup( GROUP1 );
    assertNotNull( group );
}

/**
 * Try to create a group and make sure an error is thrown.
 */
function testCreateGroup_Again() {
    tree.createGroup( GROUP1 );
    try {
        tree.createGroup( GROUP1 );
        fail( "Should have errored when trying to create an existing group." );
    } catch(e) {
        // TODO test exception type
    }
    
    var group = tree.getGroup( GROUP1 );
    assertNotNull( group );
    // TODO make sure it doesn't exist twice.
}

/** 
 * Try creating two fresh groups.
 */
function testCreateGroups() {
    tree.createGroups( [GROUP1, GROUP2] );
    
    assertNotNull( tree.getGroup( GROUP1 ) );
    assertNotNull( tree.getGroup( GROUP2 ) );
}

/**
 * Create two groups, one of which exists. DO NOT error.
 */
function testCreateGroups_GroupExists() {
    tree.createGroup( GROUP1 );
    
    try {
        tree.createGroups( [GROUP1, GROUP2] );
    } catch(e) {
        fail( "We should not have errored when a group already existed." );
    }
    
    assertNotNull( tree.getGroup( GROUP1 ) );
    assertNotNull( tree.getGroup( GROUP2 ) );
}

/**
 * Create a group and add an item to it.
 */
function testCreateItem() {
    tree.createGroup( GROUP1 );
    tree.createItem( GROUP1, JID1 );
    
    assertNotNull( tree.getItem( GROUP1, JID1 ) );
}

/**
 * Create an item in two groups
 */
function testCreateItem_DifferentGroup() {
    tree.createGroup( GROUP1 );
    tree.createItem( GROUP1, JID1 );

    tree.createGroup( GROUP2 );
    tree.createItem( GROUP2, JID1 );
    
    assertNotNull( tree.getItem( GROUP1, JID1 ) );
    assertNotNull( tree.getItem( GROUP2, JID1 ) );
}

/**
 * Try to create an item that already exists. expect an error.
 */
function testCreateItem_SameGroup() {
    tree.createGroup( GROUP1 );
    tree.createItem( GROUP1, JID1 );
    try {
        tree.createItem( GROUP1, JID1 );
        fail( "Should have errored when trying to create an existing item." );
    } catch(e) {
        // TODO test exception type
    }
}

/**
 * Creates two groups and an item in group 1. moves that item to group 1.
 * ensures that item exists in group 2 but not group 1.
 */
function testMoveItem() {
    tree.createGroup( GROUP1 );
    tree.createGroup( GROUP2 );
    
    tree.createItem( GROUP1, JID1 );
    
    tree.moveItem( GROUP1, GROUP2, JID1 );
    
    assertNull( tree.getItem( GROUP1, JID1 ) );
    assertNotNull( tree.getItem( GROUP2, JID1 ) );
}


/**
 * Creates two groups each with a jid. tries to move from group 1 to 2. errors
 * because it already exists in group 2.
 */
function testMoveItem_JidAlreadyExists() {
    tree.createGroup( GROUP1 );
    tree.createGroup( GROUP2 );
    
    tree.createItem( GROUP1, JID1 );
    tree.createItem( GROUP2, JID1 );
    
    try {
        tree.moveItem( GROUP1, GROUP2, JID1 );
        fail( "Should have errored moving to a group where jid already exists." );
    } catch(e){ 
    
    }
}

/** 
 * Test the getItemInAllGroups method by creating two groups with an item,
 * and ensuring that there are two items in the returned list
 */
function testGetItemInAllGroups() {
    tree.createGroup( GROUP1 );
    tree.createGroup( GROUP2 );
    
    tree.createItem( GROUP1, JID1 );
    tree.createItem( GROUP2, JID1 );
    
    var items = tree.getItemInAllGroups( JID1 );
    
    assertEquals( "The item does not exist in the expected number of groups", 2, items.length );
    
    // TODO ensure that the names match.
}

/**
 * Test the removeItemFromAllGroups method.
 */
function testRemoveItemFromAllGroups() {
    tree.createGroup( GROUP1 );
    tree.createGroup( GROUP2 );
    
    tree.createItem( GROUP1, JID1 );
    tree.createItem( GROUP2, JID1 );
    
    tree.removeItemFromAllGroups( JID1 );
    
    assertNull( tree.getItem( GROUP1, JID1 ) );
    assertNull( tree.getItem( GROUP2, JID1 ) );   
}

/**
 * Update the groups an item belongs to with all requisite groups existing.
 */
function testUpdateItemGroups_Group1ToGroup2_GroupsExist() {
    tree.createGroup( GROUP1 );
    tree.createGroup( GROUP2 );
    
    tree.createItem( GROUP1, JID1 );
    
    tree.updateItemGroups( JID1, [GROUP2] );
    
    assertNull( tree.getItem( GROUP1, JID1 ) );
    assertNotNull( tree.getItem( GROUP2, JID1 ) );   
}

/**
 * Update the groups an item belongs to with a requisite group missing. will
 * error.
 */
function testUpdateItemGroups_Group1ToGroup2_Group2Missing() {
    tree.createGroup( GROUP1 );
    
    tree.createItem( GROUP1, JID1 );
    
    try {
        tree.updateItemGroups( JID1, [GROUP2] ); 
        fail( "Should have errored when Group 2 didn't exist" );
    } catch(e) {
    
    }
}

/**
 * Creates an item that exists in a single group. then updates groups, including
 * the one it already exists in. should NOT error.
 */ 
function testUpdateItemGroups_Group1ToGroup1AndGroup2_GroupsExist() {
    tree.createGroup( GROUP1 );
    tree.createGroup( GROUP2 );
    
    tree.createItem( GROUP1, JID1 );
    
    try {
        tree.updateItemGroups( JID1, [GROUP1, GROUP2] ); 
        
    } catch(e) {
        fail( "Should not have errored when already existing in a group." );
    }
}

/**
 * Creates an item in group 1 and then updates it to exist in group 1 and 2. but 
 * group 2 does not exist, so an error is thrown.
 */
function testUpdateItemGroups_Group1ToGroup1AndGroup2_Group2Missing() {
    tree.createGroup( GROUP1 );
    
    tree.createItem( GROUP1, JID1 );
    
    try {
        tree.updateItemGroups( JID1, [GROUP1, GROUP2] ); 
        fail( "Should have errored when group 2 didn't exist" );
    } catch(e) {
    
    }
}

        </script>
    </body>
</html>




